package com.gitee.chili.leisure.config;

import com.gitee.chili.leisure.HeaderInterceptor;
import com.gitee.chili.leisure.HeaderProperties;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * FeignConfiguration
 *
 * @author week solider
 * @version v1.0
 * @since 2019/12/30
 */

@Slf4j
public class FeignConfiguration extends HeaderInterceptor implements RequestInterceptor {

    @Autowired
    private HeaderProperties properties;

    @Override
    public void apply(RequestTemplate template) {
        try {
            template.headers(apply(properties));
        } catch (Exception e) {
            log.error("请求头设置异常：{}", e.getMessage(), e);
        }
    }

    /**
     * web 监听器
     * 防止 {@link ServletRequestAttributes} 请求属性对象为空
     *
     * @return ServletRequestAttributes 实例
     */
    @Bean
    public RequestContextListener requestContextListener() {
        return new RequestContextListener();
    }
}
